#include "game_time.h"

BOOL __set_sys_time_utc(time_t utc_time)
{
  struct timeval tv;
  tv.tv_sec = utc_time;
  tv.tv_usec = 0;
  if (settimeofday(&tv, NULL) < 0) {
    return FALSE;
  }
  return TRUE;
}
BOOL __set_sys_time_utc_tm(tm *utc_tm)
{
  time_t utc_time = timegm(utc_tm);
  return __set_sys_time_utc(utc_time);
}
time_t __get_sys_time_utc()
{
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return (time_t)tv.tv_sec;
}
struct tm __get_sys_time_utc_tm()
{
  time_t utc_time = __get_sys_time_utc();
  struct tm tm_tmp;
  gmtime_r(&utc_time, &tm_tmp);
  return tm_tmp;
}

// 设置系统时间（本地）
BOOL __set_sys_time_local(time_t local_time)
{
  struct tm tmp_tm;
  struct tm *local_tm = localtime_r(&local_time, &tmp_tm);
  time_t utc_time = mktime(local_tm);
  return __set_sys_time_utc(utc_time);
}
BOOL __set_sys_time_local_tm(tm *local_tm)
{
  time_t local_time = mktime(local_tm); // mktime是考虑了时区的，返回UTC时间
  return __set_sys_time_utc(local_time);
}
// 获取系统时间（本地）
time_t __get_sys_time_local()
{
  time_t utc_time = __get_sys_time_utc();
  struct tm tm_tmp;
  localtime_r(&utc_time, &tm_tmp);
  time_t local_time = mktime(&tm_tmp);
  return local_time;
}
struct tm __get_sys_time_local_tm()
{
  time_t utc_time = __get_sys_time_utc();
  struct tm tm_tmp;
  localtime_r(&utc_time, &tm_tmp);
  return tm_tmp;
}
